//package com.plian.system.service.bpa.impl;
//
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.plian.Tools.*;
//import com.plian.system.config.exception.MyRuntimeException;
//import com.plian.system.dto.bpa.BpaIndustryStandardConfigDTO;
//import com.plian.system.entity.bpa.BpaIndustryStandardConfig;
//import com.plian.system.entity.sys.Dict;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.mapper.bpa.BpaIndustryStandardConfigMapper;
//import com.plian.system.mapper.sys.DictMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ListResult;
//import com.plian.system.model.ObjResult;
//import com.plian.system.model.R;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.mybatis.support.Query;
//import com.plian.system.service.bpa.IBpaIndustryStandardConfigService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.vo.bpa.BpaIndustryStandardConfigVO;
//import com.plian.system.wrapper.bpa.BpaIndustryStandardConfigWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Optional;
//
///**
// * @author jiatp
// * @create 2022-12-14
// **/
//@Slf4j
//@Service
//public class BpaIndustryStandardConfigServiceImpl extends ServiceImpl<BpaIndustryStandardConfigMapper, BpaIndustryStandardConfig> implements IBpaIndustryStandardConfigService {
//
//
//    @Resource
//    private BpaIndustryStandardConfigWrapper industryStandardConfigWrapper;
//    @Resource
//    private DictMapper dictMapper;
//    @Resource
//    private IOrgService orgService;
//
//
//    @Override
//    @Transactional(rollbackFor = Throwable.class)
//    public R submit(BpaIndustryStandardConfigVO industryStandardConfigVO) {
//        try {
//
//            BpaIndustryStandardConfig industryStandardConfig = industryStandardConfigWrapper.voToEntity(industryStandardConfigVO);
//            //行业code
//            Dict dict1 = dictMapper.selectOne(new LambdaQueryWrapper<Dict>().eq(Dict::getCode, industryStandardConfigVO.getIndustryTypeCode()));
//
//            List<Org> orgList = orgService.findThirteenOrgOfBackend().getList();
//
//            StringBuffer sbName = new StringBuffer();
//            StringBuffer sbIds = new StringBuffer();
//            List<String> suitableUnitCodes = industryStandardConfigVO.getSuitableUnitCodes();
//            for (String suitableUnitCode : suitableUnitCodes) {
//                Optional<Org> orgOptional = orgList.stream().filter(item -> item.getId().equals(suitableUnitCode)).findFirst();
//                String suitableUnitName = orgOptional.isPresent() ? orgOptional.get().getName() : "";
//                String suitableUnitId = orgOptional.isPresent() ? orgOptional.get().getId() : "";
//                sbName.append(suitableUnitName + "、");
//                sbIds.append(suitableUnitId + "、");
//            }
//            industryStandardConfig.setSuitableUnitId(sbIds.toString().substring(0, sbIds.toString().length() - 1).toString());
//            industryStandardConfig.setSuitableUnitName(sbName.toString().substring(0, sbName.toString().length() - 1).toString());
//            if (StringUtil.isBlank(industryStandardConfig.getId()) || industryStandardConfig == null) {
//                industryStandardConfig.setId(UuidUtil.get32UUID());
//                industryStandardConfig.setCreateTime(DateUtil.parse(industryStandardConfigVO.getModifyTime(), DateUtil.PATTERN_DATETIME));
//                industryStandardConfig.setUpdateTime(DateUtil.parse(industryStandardConfigVO.getModifyTime(), DateUtil.PATTERN_DATETIME));
//                industryStandardConfig.setIndustryTypeName(dict1.getName());
//                industryStandardConfig.setCreateUser(TokenUtil.getTokenUserId());
//                industryStandardConfig.setUpdateUser(TokenUtil.getTokenUserId());
//                baseMapper.insert(industryStandardConfig);
//                log.info("succes:行业配置新增成功...");
//            } else {
//
//                industryStandardConfig.setUpdateTime(DateUtil.parse(industryStandardConfigVO.getModifyTime(), DateUtil.PATTERN_DATETIME));
//                industryStandardConfig.setUpdateUser(TokenUtil.getTokenUserId());
//                industryStandardConfig.setIndustryTypeName(dict1.getName());
//                baseMapper.updateById(industryStandardConfig);
//                log.info("succes:行业配置修改成功...");
//            }
//            return R.success();
//        } catch (Exception e) {
//            log.error("error-submit:{}", e.getMessage());
//            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
//        }
//
//
//    }
//
//    @Transactional(rollbackFor = Throwable.class)
//    @Override
//    public R remove(BpaIndustryStandardConfigVO industryStandardConfigVO) {
//        try {
//            baseMapper.deleteBatchIds(industryStandardConfigVO.getIds());
//            return R.success();
//        } catch (Exception e) {
//            throw new MyRuntimeException(new ObjResult(CommonCode.FAIL));
//        }
//    }
//
//    @Override
//    public ListResult list(Query query) {
//        ListResult<BpaIndustryStandardConfigDTO> listResult;
//        try {
//            List<SearchEntity> search = query.getSearch();
//            BpaIndustryStandardConfigVO bpaIndustryStandardConfigVO = new BpaIndustryStandardConfigVO();
//            if (CollectionUtil.isNotEmpty(search)) {
//                for (SearchEntity entity : search) {
//                    if ("industryTypeCode".equals(entity.getField())) {
//                        bpaIndustryStandardConfigVO.setIndustryTypeName(entity.getValue().toString());
//                    }
//                    if ("suitableUnitId".equals(entity.getField())) {
//                        bpaIndustryStandardConfigVO.setSuitableUnitId(entity.getValue().toString());
//                    }
//                    if ("isYear".equals(entity.getField())) {
//                        bpaIndustryStandardConfigVO.setIsYear(entity.getValue().toString());
//                    }
//                }
//            }
//            IPage<BpaIndustryStandardConfigDTO> pages = baseMapper.pageList(Condition.getPage(query), bpaIndustryStandardConfigVO);
//            listResult = new ListResult<>(CommonCode.SUCCESS);
//            listResult.setList(pages.getRecords());
//            listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            throw new MyRuntimeException(new ObjResult(CommonCode.ERROR));
//        }
//        return listResult;
//    }
//
//    @Override
//    public R detail(BpaIndustryStandardConfigVO industryStandardConfigVO) {
//        BpaIndustryStandardConfig industryStandardConfig = null;
//        try {
//            industryStandardConfig = baseMapper.selectById(industryStandardConfigVO.getId());
//            BpaIndustryStandardConfigVO bpaIndustryStandardConfigVO = industryStandardConfigWrapper.entityToVO(industryStandardConfig);
//            String suitableUnitId = industryStandardConfig.getSuitableUnitId();
//            List<String> subtTaList = Arrays.asList(suitableUnitId.split("、"));
//            bpaIndustryStandardConfigVO.setSuitableUnitCodes(subtTaList);
//            return R.data(bpaIndustryStandardConfigVO, CommonCode.SUCCESS);
//        } catch (Exception e) {
//            log.error("error-deatil:查询详情失败{}", e.getMessage());
//            return R.data(industryStandardConfig, CommonCode.FAIL);
//        }
//
//    }
//}
